﻿using Ops.Mpc.States;
using Ops.Mpc.ExampleApplication.ExampleService;
using Ops.Mpc.Events;
using Ops.Mpc.ExampleApplication.SimpleBindings.Models;
using Ops.Mpc.ExampleApplication.Models;
using Ops.Mpc.Ioc;
using Ops.Mpc.Ioc.MessageBoxServices;
using System.Windows;
using Ops.Mpc.Models;

namespace Ops.Mpc.ExampleApplication.SimpleBindings.Actions
{
    public class CreatePersonStateAction : StateAction
    {
        #region Fields
        private SimpleModel _simpleModel = null;
        #endregion

        public override void Start()
        {
            base.Start();
            _simpleModel = this.GetModel<SimpleModel>();
        }

        protected override void OnExecute(object context)
        {
            base.OnExecute(context);

            this.SetBusyMessage("Creating {0} {1}",_simpleModel.FirstName,_simpleModel.LastName);

            IocServiceLocator.Get<IExampleServiceContext>().CreatePerson(_simpleModel.FirstName,_simpleModel.LastName,OnGetPersonCompleted,OnError);
        }

        private void OnGetPersonCompleted(Person person, object context)
        {
            _simpleModel.Set(person);

            IocServiceLocator.Get<IMessageBoxService>().Show(string.Format("{0} was created", person.LastNameFirstName), "Success", MessageBoxButton.OK);

            this.Finish();
        }
    }
}
